// PlatformView.cpp : implementation of the CPlatformView class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "ChildView.h"

//////////////////////////////////////////////////////////////////////////

void QTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	DWORD dwStyle = GetStyle();

	HDC hdc = lpDrawItemStruct->hDC;
	RECT & rc = lpDrawItemStruct->rcItem;
	if (lpDrawItemStruct->itemAction == ODA_DRAWENTIRE) {
		UIgdi::FillSolidRect(hdc, &rc, RGB(25,25,25));
		int nItemCount = GetItemCount();
		if (nItemCount > 0) {
			RECT rcItem;
			if (GetItemRect(nItemCount-1, &rcItem)) {
				if (dwStyle & TCS_BOTTOM) {
					RECT rcPane = rc;
					rcPane.bottom = rcItem.top;
					UIgdi::FillSolidRect(hdc, &rcPane, RGB(255,255,255));
					UIgdi::DrawLine(hdc, rcPane.left, rcPane.bottom, rcPane.right, rcPane.bottom, PS_SOLID, 1, RGB(0,0,0), R2_COPYPEN);
				}
			}
		}
	} else {
		CDCHandle dc(hdc);

		TCHAR szText[MAX_PATH] = {0};
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_TEXT|TCIF_IMAGE;
		tcItem.pszText = szText;
		tcItem.cchTextMax = MAX_PATH;
		GetItem(lpDrawItemStruct->itemID, &tcItem);

		bool bSelected = (CDIS_SELECTED == (lpDrawItemStruct->itemState & ODS_SELECTED));
		bool bFocus = (CDIS_FOCUS == (lpDrawItemStruct->itemState & ODS_FOCUS));

		dc.SetBkMode(TRANSPARENT);

		HPEN hOldPen = dc.SelectStockPen(BLACK_PEN);
		COLORREF crOldText = CLR_INVALID;
		if(bSelected) {
			POINT pts[4];
			_GetTrapezoid(rc, pts);
			CRgn rgn;
			rgn.CreatePolygonRgn(pts, 4, WINDING);

			CBrush brush;
			brush.CreateSolidBrush(rand());
			dc.FillRgn(rgn, brush);

			dc.Polyline(&pts[0], 4);

			crOldText = dc.SetTextColor(RGB(255,255,255));
		} else {
			crOldText = dc.SetTextColor(RGB(92,92,92));
		}

		dc.DrawText(szText, _tcslen(szText), &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

		dc.SetTextColor(crOldText);

		dc.SelectPen(hOldPen);
	}
}

//////////////////////////////////////////////////////////////////////////

CChildView::CChildView() : m_cxTB(0)
{
	m_bDestroyPageOnRemove = 0;
}

BOOL CChildView::PreTranslateMessage(MSG* pMsg)
{
	return FALSE;
}

BOOL CChildView::OnEraseBkgnd(HDC hdc)
{
	CRect rcClient;
	GetClientRect(&rcClient);

	UIgdi::FillSolidRect(hdc, &rcClient, RGB(25,25,25));
	return TRUE;
}

int CChildView::CalcTabHeight()
{
	return Base::CalcTabHeight();
}

bool CChildView::CreateTabControl()
{
	bool bRet = Base::CreateTabControl();
	if(bRet)
	{
		m_tab.ModifyStyle( 0, TCS_BOTTOM|TCS_OWNERDRAWFIXED);
		m_wndTab.SubclassWindow(m_tab);

		/*DWORD dwStyle = (ATL_SIMPLE_TOOLBAR_STYLE | TBSTYLE_FLAT | CCS_NODIVIDER | CCS_NORESIZE | CCS_NOPARENTALIGN) & ~WS_VISIBLE;
		m_wndTB = AtlCreateSimpleToolBar(m_hWnd, IDR_MAINFRAME, FALSE, dwStyle);

		// BLOCK: set drop-down style for the window button
		{
			TBBUTTONINFO tbbi;
			tbbi.cbSize = sizeof(tbbi);
			tbbi.dwMask = TBIF_STYLE;
			tbbi.fsStyle = BTNS_BUTTON | BTNS_WHOLEDROPDOWN;
			m_wndTB.SetButtonInfo(ID_WINDOW_SHOW_VIEWS, &tbbi);
		}

		RECT rect = { 0 };
		m_wndTB.GetItemRect(m_wndTB.GetButtonCount() - 1, &rect);
		m_cxTB = rect.right;*/

		CToolTipCtrl tt = m_tab.GetTooltips();
		tt.SetMaxTipWidth(0);   // we just want to use \n for a new line
	}

	return bRet;
}

void CChildView::UpdateLayout()
{
	RECT rect;
	GetClientRect(&rect);

	if(m_tab && m_tab.IsWindowVisible())
		m_tab.SetWindowPos(NULL, 0, rect.bottom-m_cyTabHeight, rect.right - rect.left - m_cxTB, m_cyTabHeight, SWP_NOZORDER);

	if(m_wndTB && m_tab.IsWindowVisible())
		m_wndTB.SetWindowPos(NULL, rect.right - m_cxTB, rect.bottom-m_cyTabHeight, m_cxTB, m_cyTabHeight, SWP_NOZORDER);

	if(m_nActivePage != -1)
		::SetWindowPos(GetPageHWND(m_nActivePage), NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top - m_cyTabHeight, SWP_NOZORDER);
}

void CChildView::ShowTabControl(bool bShow)
{
	m_tab.ShowWindow(bShow ? SW_SHOWNOACTIVATE : SW_HIDE);
	if (m_wndTB) {
		m_wndTB.ShowWindow(bShow ? SW_SHOWNOACTIVATE : SW_HIDE);
	}
}

void CChildView::UpdateTooltipText(LPNMTTDISPINFO pTTDI)
{
	ATLASSERT(pTTDI != NULL);

	m_strTooltip = GetPageTitle(pTTDI->hdr.idFrom);

	CAxWindow wnd = GetPageHWND(pTTDI->hdr.idFrom);
	CComPtr<IWebBrowser2> spWebBrowser;
	HRESULT hRet = wnd.QueryControl(IID_IWebBrowser2, (void**)&spWebBrowser);
	if(spWebBrowser != NULL)
	{
		BSTR bstrURL;
		spWebBrowser->get_LocationURL(&bstrURL);
		m_strTooltip += _T("\n");
		m_strTooltip += bstrURL;
	}

	pTTDI->lpszText = (LPTSTR)(LPCTSTR)m_strTooltip;
}
